import { _decorator, Collider2D, Color, Component, Contact2DType, Node, Sprite, Vec3 } from 'cc';
import { Core } from '../core/Core';
import { Context } from './Context';
import { EGameEvent, EGroup, EPlayerWeaponTag, EprefabName } from '../core/Config';
import { NumberText } from './NumberText';
import { CommonEffect } from './CommonEffect';
const { ccclass, property } = _decorator;

@ccclass('Monster')
export class Monster extends Component {

    private _aniNode: Node;
    private _animationSprite: Sprite=null;

    speed: number = 2;
    direction: Vec3 = new Vec3();
    isMoving: boolean = false;

    ap: number = 5;
    hp: number = 20;
    dp: number = 2;

    protected onLoad(): void {
        //初始化动画节点
        this._aniNode = this.node.getChildByName('Animation');
        this._animationSprite = this._aniNode.getComponent(Sprite);
    }
    protected onEnable(): void {
        this.schedule(() => {
            let dest = Context.player.node.worldPosition.clone();
            dest.subtract(this.node.worldPosition).normalize();
            this.direction.set(dest);
            this._aniNode.setScale(this.direction.x >= 0 ? 1 : -1, 1);
        }, 0.1);

        let collider = this.node.getComponent(Collider2D);
        collider?.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        collider?.on(Contact2DType.END_CONTACT, this.onEndContact, this);
    }
    protected onDisable(): void {
        this.unscheduleAllCallbacks();
        let collider = this.node.getComponent(Collider2D);
        collider?.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        collider?.off(Contact2DType.END_CONTACT, this.onEndContact, this);
    }

    start() {

    }

    update(deltaTime: number) {
        if (!this.isMoving) {
            return;
        }
        this.node.setPosition(
            this.node.position.x + this.direction.x * this.speed,
            this.node.position.y + this.direction.y * this.speed,
        );
    }

    hurt(value: number) {
        this.hp-=value;
        if (this.hp > 0) {
            this.slashOnce();
            NumberText.show(`${Math.ceil(value)}`, this.node.worldPosition);
        }else {
            //死亡
            this.hp = 0;
            Core.instance.event.emit(EGameEvent.MONSTER_DEAD,this.node);
            let deadEffect=Core.instance.creatNode(EprefabName.MONSTER_DEAD,Context.texts)
            deadEffect.setWorldPosition(this.node.worldPosition);
            let effect=deadEffect.getComponent(CommonEffect);
            effect.show('MonsterDead');
            this.node.destroy();
        }
    }
    
    private slashOnce() {
        this._animationSprite.color = Color.RED;
        this.scheduleOnce(() =>this._animationSprite.color=Color.WHITE, 0.15);
    }

    private onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
        switch (otherCollider.group) {
            case EGroup.PLAYER:
                Context.player.hurt(this.ap - Core.instance.playerData.dp);
                break;
            case EGroup.PLAYER_WEAPON:
                this.onWeaponContact(otherCollider);
                break;
            default:
                break;


        }
    }
    private onEndContact(selfCollider: Collider2D, otherCollider: Collider2D) {

    }
    private onWeaponContact(otherCollider: Collider2D) {
        let damage = Core.instance.playerData.ap - this.dp;
        switch (otherCollider.tag) {
            case EPlayerWeaponTag.ARROW:
                console.log('arrow');
                break;
            case EPlayerWeaponTag.BLADE:
                break;
            case EPlayerWeaponTag.ENERGY_BALL:
                break;
            default:
                break;
        }
        this.hurt(damage);
    }
}


